package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RecordCatalogue;
import com.ruoyi.system.domain.RecordPerson;
import com.ruoyi.system.mapper.RecordPersonMapper;
import com.ruoyi.system.service.IRecordPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 卷内目录Service业务层处理
 *
 * @author admin
 * @date 2022-02-20
 */
@Service
public class RecordPersonServiceImpl implements IRecordPersonService {
    @Autowired
    private RecordPersonMapper recordPersonMapper;

    /**
     * 查询卷内目录
     *
     * @param personId 卷内目录主键
     * @return 卷内目录
     */
    @Override
    public RecordPerson selectRecordPersonByPersonId(Long personId) {
        return recordPersonMapper.selectRecordPersonByPersonId(personId);
    }

    /**
     * 查询卷内目录列表
     *
     * @param recordPerson 卷内目录
     * @return 卷内目录
     */
    @Override
    public List<RecordPerson> selectRecordPersonList(RecordPerson recordPerson) {
        return recordPersonMapper.selectRecordPersonList(recordPerson);
    }

    /**
     * 新增卷内目录
     *
     * @param recordPerson 卷内目录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecordPerson(RecordPerson recordPerson) {
        recordPerson.setCreateBy(SecurityUtils.getUsername());
        recordPerson.setCreateTime(DateUtils.getNowDate());
        int rows = recordPersonMapper.insertRecordPerson(recordPerson);
        insertRecordCatalogue(recordPerson);
        return rows;
    }

    /**
     * 修改卷内目录
     *
     * @param recordPerson 卷内目录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecordPerson(RecordPerson recordPerson) {
        recordPerson.setUpdateBy(SecurityUtils.getUsername());
        recordPerson.setUpdateTime(DateUtils.getNowDate());
        recordPersonMapper.deleteRecordCatalogueByPersonId(recordPerson.getPersonId());
        insertRecordCatalogue(recordPerson);
        return recordPersonMapper.updateRecordPerson(recordPerson);
    }

    /**
     * 批量删除卷内目录
     *
     * @param personIds 需要删除的卷内目录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordPersonByPersonIds(Long[] personIds) {
        recordPersonMapper.deleteRecordCatalogueByPersonIds(personIds);
        return recordPersonMapper.deleteRecordPersonByPersonIds(personIds);
    }

    /**
     * 删除卷内目录信息
     *
     * @param personId 卷内目录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordPersonByPersonId(Long personId) {
        recordPersonMapper.deleteRecordCatalogueByPersonId(personId);
        return recordPersonMapper.deleteRecordPersonByPersonId(personId);
    }

    /**
     * 新增数据挂接信息
     *
     * @param recordPerson 卷内目录对象
     */
    public void insertRecordCatalogue(RecordPerson recordPerson) {
        List<RecordCatalogue> recordCatalogueList = recordPerson.getRecordCatalogueList();
        Long personId = recordPerson.getPersonId();
        if (StringUtils.isNotNull(recordCatalogueList)) {
            List<RecordCatalogue> list = new ArrayList<RecordCatalogue>();
            for (RecordCatalogue recordCatalogue : recordCatalogueList) {
                recordCatalogue.setPersonId(personId);
                list.add(recordCatalogue);
            }
            if (list.size() > 0) {
                recordPersonMapper.batchRecordCatalogue(list);
            }
        }
    }
}
